<?php
require_once ($_SESSION ['pathway'] . "/commun/ouvre_base.php");

// ~ $hash_champ_CCFS_a_rentrer = array();
// ~ if(isset($_SESSION['hash_CCFS'])){ $hash_champ_CCFS_a_rentrer = $_SESSION['hash_CCFS']; }
// ~
// ~ //remplissage des valeurs dans la table CCFS
// ~ if(isset($_POST['hand']) and $hash_champ_CCFS_a_rentrer['hand'] != ''){ $hash_champ_CCFS_a_rentrer['hand'] = $_POST['hand']; }
// ~ if(isset($_POST['pegboard_timing']) and $hash_champ_CCFS_a_rentrer['pegboard_timing'] != ''){ $hash_champ_CCFS_a_rentrer['pegboard_timing'] = $_POST['pegboard_timing']; }
// ~ if(isset($_POST['clicktest_timing']) and $hash_champ_CCFS_a_rentrer['clicktest_timing'] != ''){ $hash_champ_CCFS_a_rentrer['clicktest_timing'] = $_POST['clicktest_timing']; }
// ~
// ~ if(isset($_POST['pegboard_timing']) and isset($_POST['clicktest_timing'])){
// ~ $pegboard_timing = $_POST['pegboard_timing'];
// ~ $clicktest_timing = $_POST['clicktest_timing'];
// ~
// ~ require_once("./requetes/recupere_age.php");
// ~ echo $age;
// ~ $hash_champ_CCFS_a_rentrer['score_pegboard'] = $hash_champ_CCFS_a_rentrer['pegboard_timing'] - (13.4 - 0.16*$age + 0.002*$age*$age);
// ~ $hash_champ_CCFS_a_rentrer['score_clicktest'] = $hash_champ_CCFS_a_rentrer['clicktest_timing'] - (8+0.05*$age);
// ~ $hash_champ_CCFS_a_rentrer['score_CCFS'] = log10(7 + $hash_champ_CCFS_a_rentrer['score_pegboard']/10 + 4*$hash_champ_CCFS_a_rentrer['score_clicktest']/10);
// ~ }
// ~
// ~ //remplissage du champ "score_CCFS" dans diagnostic
// ~ $query_remplissage_score_CCFS = "UPDATE `$db`.`diagnostic` SET `score_CCFS` = '".$hash_champ_CCFS_a_rentrer['score_CCFS']."' WHERE `diagnostic`.`form` = ".$_SESSION['form'];
// ~ mysql_query($query_remplissage_score_CCFS) or die("impossible de remplir le score CCFS : " . mysql_error());
// ~ #mysqli_query($query_remplissage_score_CCFS) or die("impossible de remplir le score CCFS : " . mysql_error());
// ~
// ~ $hash_champ_CCFS_a_rentrer = array(
// ~ "form"=> $_SESSION['form'],
// ~ "hand"=> $hash_champ_CCFS_a_rentrer['hand'],
// ~ "pegboard_timing"=> $hash_champ_CCFS_a_rentrer['pegboard_timing'],
// ~ "clicktest_timing"=> $hash_champ_CCFS_a_rentrer['clicktest_timing'],
// ~ "score_pegboard"=> $hash_champ_CCFS_a_rentrer['score_pegboard'],
// ~ "score_clicktest"=> $hash_champ_CCFS_a_rentrer['score_clicktest'],
// ~ "score_CCFS"=> $hash_champ_CCFS_a_rentrer['score_CCFS']);
// ~
// ~ foreach($hash_champ_CCFS_a_rentrer as $key => $value){
// ~ if($value != ''){
// ~ $query_update_form = "UPDATE `".$db."`.`ccfs` SET `$key` = '$value' WHERE `ccfs`.`form` = ".$_SESSION['form'];
// echo $query_update_form."<br />";
// ~ mysql_query($query_update_form) or die("impossible d'updater la form : " . mysql_error());
// ~ #mysqli_query($query_update_form) or die("impossible d'updater la form : " . mysql_error());
// ~ }else{
// ~ $query_update_form = "UPDATE `".$db."`.`ccfs` SET `$key` = NULL WHERE `ccfs`.`form` = ".$_SESSION['form'];
// ~ mysql_query($query_update_form) or die("impossible d'updater la form : " . mysql_error());
// ~ #mysqli_query($query_update_form) or die("impossible d'updater la form : " . mysql_error());
// ~ }
// ~ }
// ~

if (isset ( $_GET ['from'] ) and $_GET ['from'] == 'formspatax') {
	$hash_champ_CCFS_a_rentrer = $_SESSION ['hash_CCFS'];
} else {
	
	// remplissage des valeurs dans la table CCFS
	if (isset ( $_POST ['hand'] ) and $_POST ['hand'] != '') {
		$hand = $_POST ['hand'];
	} else {
		$hand = '';
	}
	if (isset ( $_POST ['pegboard_timing'] ) and $_POST ['pegboard_timing'] != '') {
		$pegboard_timing = $_POST ['pegboard_timing'];
	} else {
		$pegboard_timing = '';
	}
	if (isset ( $_POST ['clicktest_timing'] ) and $_POST ['clicktest_timing'] != '') {
		$clicktest_timing = $_POST ['clicktest_timing'];
	} else {
		$clicktest_timing = '';
	}
	
	if ($pegboard_timing != '' and $clicktest_timing != '') {
		require_once ("./requetes/recupere_age.php");
		$score_pegboard = $pegboard_timing - (13.4 - 0.16 * $age + 0.002 * $age * $age);
		$score_clicktest = $clicktest_timing - (8 + 0.05 * $age);
		$score_CCFS = log10 ( 7 + $score_pegboard / 10 + 4 * $score_clicktest / 10 );
	} else {
		$score_pegboard = '';
		$score_clicktest = '';
		$score_CCFS = '';
	}
	
	$hash_champ_CCFS_a_rentrer = array (
			"form" => $_SESSION ['form'],
			"hand" => $hand,
			"pegboard_timing" => $pegboard_timing,
			"clicktest_timing" => $clicktest_timing,
			"score_pegboard" => $score_pegboard,
			"score_clicktest" => $score_clicktest,
			"score_CCFS" => $score_CCFS 
	);
	
	$_SESSION ['score_CCFS'] = $score_CCFS;
	
	// remplissage du champ "score_CCFS" dans ccfs
	foreach ( $hash_champ_CCFS_a_rentrer as $key => $value ) {
		if ($value != '') {
			$value = mysql_real_escape_string ( $value );
			// ~ $value = mysqli_real_escape_string($link,$value);
			$value = str_replace ( ',', '.', $value );
			$query_update_form = "UPDATE `" . $db . "`.`ccfs` SET `$key` = '$value' WHERE `ccfs`.`form` = " . $_SESSION ['form'];
			// ~ // echo $query_update_form."<br />";
			mysql_query ( $query_update_form ) or die ( "impossible d'updater la form : " . mysql_error () );
			// ~ mysqli_query($link,$query_update_form) or die("impossible d'updater la form : " . mysql_error());
		} else {
			$query_update_form = "UPDATE `" . $db . "`.`ccfs` SET `$key` = NULL WHERE `ccfs`.`form` = " . $_SESSION ['form'];
			mysql_query ( $query_update_form ) or die ( "impossible d'updater la form : " . mysql_error () );
			// ~ mysqli_query($query_update_form) or die("impossible d'updater la form : " . mysql_error());
		}
	}
	
	// remplissage du champ "score_CCFS" dans diagnostic
	$query_remplissage_score_CCFS = "UPDATE `$db`.`diagnostic` SET `score_CCFS` = '" . $hash_champ_CCFS_a_rentrer ['score_CCFS'] . "' WHERE `diagnostic`.`form` = " . $_SESSION ['form'];
	mysql_query ( $query_remplissage_score_CCFS ) or die ( "impossible de remplir le score CCFS : " . mysql_error () );
	// ~ mysqli_query($link,$query_remplissage_score_CCFS) or die("impossible de remplir le score CCFS : " . mysql_error());
}

?>
